import { InputProps } from 'antd';
import {
  ProFormText,
  ProFormTextArea,
  ProFormDigit,

  // ProFormDependency,
} from '@ant-design/pro-form';
import utils from '@/utils/index';

type formProps = {
  id?: number;
  name?: string;
  label?: string;
  busType?: number; //{1:用户}
  fieldType?: number; //{1:文本框，2：单选，3：多选，4：日期}
  isDefault?: number;
  isRequire?: number;
  isEnable?: number;
  fieldOption?: string;
  describe?: string;
  createDate?: string;
  order?: number;
  max?: number;
  min?: number;
  regEx?: string;
  precision?: number; //保留几位小数
  disabled?: any;
  fieldProps?: InputProps & any;
  rule?: any;
  placeholder: string;
};

export default (props: formProps) => {
  const {
    label,
    isEnable,
    fieldType,
    isDefault,
    name,
    id,
    isRequire,
    regEx,
    min,
    max,
    precision,
    fieldOption,
    disabled,
    fieldProps = {},
    rule = [],
    ...restProps
  } = props;
  let _name = isDefault ? name : `custom_${id}_${fieldType}`;

  //验证修改
  !!isRequire &&
    rule.push({
      required: true,
      message: `请输入${label}`,
    });
  !!min &&
    fieldType !== 2 &&
    rule.push({
      min,
      message: `最少输入${min}位字符!`,
    });
  !!max &&
    fieldType !== 2 &&
    rule.push({
      max,
      message: `最多输入${max}位字符!`,
    });
  !!regEx &&
    rule.push({
      ...utils.constant.REG_EX_LIST[regEx],
    });
  !isEnable && (rule.length = 0);
  //组件配置修改
  fieldProps.disabled = disabled;
  fieldType === 2 && (fieldProps.precision = precision);
  // console.log(props);
  switch (fieldType) {
    case 1:
      return (
        <ProFormText
          label={label}
          rules={rule}
          preserve
          name={_name}
          fieldProps={fieldProps}
          {...restProps}
        />
      );
    case 2:
      return (
        <ProFormDigit preserve label={label} name={_name} fieldProps={fieldProps} {...restProps} />
      );
    case 3:
      return (
        <ProFormText.Password
          preserve
          label={label}
          rules={rule}
          name={_name}
          fieldProps={fieldProps}
          {...restProps}
        />
      );
    case 4:
      return (
        <ProFormTextArea
          preserve
          label={label}
          rules={rule}
          name={_name}
          fieldProps={fieldProps}
          {...restProps}
        />
      );
    default:
      return (
        <ProFormText
          preserve
          label={label}
          rules={rule}
          name={_name}
          fieldProps={fieldProps}
          {...restProps}
        />
      );
  }
};
